package com.zhifa.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhifa.entity.Admin;
import com.zhifa.entity.Role;
import com.zhifa.service.AdminService;
import com.zhifa.service.RoleService;

@Controller
@RequestMapping(value="/admin")
public class AdminController {
	
	@Resource
	private AdminService adminService;
	
	@Resource
	private RoleService roleService;
	
	@RequestMapping("/login")
	public String login(Admin admin,HttpServletRequest request){
		Admin resultAdmin=adminService.login(admin);
		if(resultAdmin==null){
			request.setAttribute("admin", admin);
			request.setAttribute("error", "用户名或密码错误,请重新填写！");
			return "login";
		}else{
			Role currentrole = roleService.findById(resultAdmin.getRoleid());
			
			HttpSession session=request.getSession();
			session.setAttribute("currentAdmin", resultAdmin);
			session.setAttribute("currentrole", currentrole);
			return "redirect:/main.jsp";
		}
	}

}
